using System;
using System.Collections;
using System.Collections.Generic;
using TMPro;
using UnityEngine;
using Utils;

public class Consumable : MonoBehaviour
{
    public int price = 1;
    public int hpr = 1;
    public int mpr = 10;
    public bool hpConsumable, mpConsumable;

    private TextMeshPro priceText;
    private Collider2D trigger;
    private GameObject iconGO;

    private void Awake()
    {
        priceText = UITool.GetComponent<TextMeshPro>("PriceText", gameObject);
        trigger = GetComponent<Collider2D>();
        iconGO = UITool.GetChildGameObject("Icon", gameObject);
        priceText.text = price.ToString();
        if (priceText.text.Equals("0"))
        {
            priceText.enabled = false;
        }
        
        InitEvent();
    }

    private void InitEvent()
    {
#if UNITY_ANDROID
        
#endif
    }

    private void OnDestroy()
    {
#if UNITY_ANDROID
        EasyButton.On_ButtonUp -= OnFuncBtnUp;
#endif
    }

    private void OnTriggerStay2D(Collider2D other)
    {
        if (Input.GetKey(KeyCode.E) && other.gameObject.layer.Equals(9))
        {
            BuyConsumable();
        }
    }

    private void OnTriggerEnter2D(Collider2D other)
    {
#if UNITY_ANDROID
        if(other.gameObject.layer.Equals(9)) {
            GameController.Instance().panelCtr.phonePanel.SwitchBtnsActive(true);
                EasyButton.On_ButtonUp += OnFuncBtnUp;
        }
#endif
    }

    private void OnTriggerExit2D(Collider2D other)
    {
#if UNITY_ANDROID
        if(other.gameObject.layer.Equals(9)) {
            GameController.Instance().panelCtr.phonePanel.SwitchBtnsActive(false);
            EasyButton.On_ButtonUp -= OnFuncBtnUp;
        }
#endif
    }

    private void OnFuncBtnUp(string btn_name)
    {
        if (!btn_name.Equals("FuncBtn"))
            return;
        BuyConsumable();
    }
    
    private void BuyConsumable()
    {
        if (GameController.Instance().archiveCtr.TempLevelData.goldNum < price) return;
        print("You Buy and Use the Consumable -- Success");
        GameController.Instance().archiveCtr.TempLevelData.goldNum -= price;
        if (hpConsumable) GameController.Instance().playerCtr.Player.SetHP(hpr);
        if (mpConsumable) GameController.Instance().playerCtr.Player.SetMP(mpr);
        iconGO.SetActive(false);
        trigger.enabled = false;
    }
}